<?php
require "common.php";

class putFile{
    private $common="";
    private $file_root_url="./file";
    public function __construct()
    {
        $this->common=new Common();
    }

    public function put(){
        $sql="SELECT stuid,d0,d1,d2,d3,blokType,`区块类型`,`正文标题`,`正文内容` FROM lunwen_yuanfile WHERE user_info_id='".$_SESSION["id"]."' and is_del=0 ORDER BY d1 desc,d2 desc,d3 desc";
        $query=mysqli_query($this->common->startMysql(),$sql);
        $info_arr=mysqli_fetch_all($query,MYSQLI_ASSOC);
        if (!$info_arr) Common::rf("无数据");
        $xls="<style> .text { mso-number-format:\@; } </script> <div>";
        $xls.='<table cellspacing="0" cellpadding="4" align="center" border="0" id="GridView3" style="color:#333333;border-collapse:collapse;">';
        $xls.='<tr style="color:White;background-color:#1C5E55;font-weight:bold;">
			<th scope="col">stuid</th><th scope="col">d0</th><th scope="col">d1</th><th scope="col">d2</th><th scope="col">d3</th><th scope="col">blokType</th><th scope="col">区块类型</th><th scope="col">正文标题</th><th scope="col">正文内容</th>
		</tr>';
        foreach ($info_arr as $info_arr_k=>$info_arr_v){

            if ($info_arr_k%2==0){
                //偶数
                $xls.='<tr style="background-color:#E3EAEB;">';
            }else{
                $xls.='<tr style="background-color:White;">';
            }
            $xls.='<td class="text">'.$info_arr_v["stuid"].'</td><td>'.$info_arr_v["d0"].'</td><td>'.$info_arr_v["d1"].'</td><td>'.$info_arr_v["d2"].'</td><td>'.$info_arr_v["d3"].'</td><td>'.$info_arr_v["blokType"].'</td><td style="width:80px;">'.$info_arr_v["区块类型"].'</td><td style="width:100px;">'.$info_arr_v["正文标题"].'</td><td>'.$info_arr_v["正文内容"].'</td>';
            $xls.='</tr>';

        }
        $xls.="</div>";

        $user_dir_url=$this->file_root_url."/".$_SESSION["id"];
        $file_url=$user_dir_url."/源文件.xls";
        $file_url=iconv('utf-8', 'gbk', $file_url);
        if(!is_dir($user_dir_url)){
            mkdir($user_dir_url);
        }
        file_put_contents($file_url,iconv('UTF-8','GBK',$xls));

        $file = fopen ( $file_url, "rb" );

        Header ( "Content-Disposition: attachment; filename=源文件.xls");
        header('Content-type: application/txt');
        header('Content-Transfer-Encoding: binary');
        header('Content-Description: File Transfer');
        header('Content-Transfer-Encoding: binary');
        header('Cache-Control: must-revalidate');
        ob_clean();
        flush();

        //读取文件内容并直接输出到浏览器
        echo fread ( $file, filesize ( $file_url ) );
        fclose ( $file );
        exit ();
    }

}

$putFileClass=new putFile();
$putFileClass->put();